﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;

namespace Sale_Operation
{
    public class UpLoadOp
    {
        public static List<string> Upload()
        {
            List<string> FileNameList = new List<string>();
            string NewName = String.Empty;
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            //以下为创建图库目录
            string dirpath = HttpContext.Current.Server.MapPath("../Upload");

            if (Directory.Exists(dirpath) == false)
            {
                Directory.CreateDirectory(dirpath);
            }
            Random ro = new Random();
            int name = 1;
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile myFile = files[i];
                string FileName = "";
                string FileExtention = "";
                FileName = System.IO.Path.GetFileName(myFile.FileName);
                string stro = ro.Next(100, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片
                NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
                if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
                {
                    FileExtention = System.IO.Path.GetExtension(myFile.FileName);

                    string ppath = dirpath + @"\" + NewName + FileExtention;
                    NewName += FileExtention;
                    myFile.SaveAs(ppath);
                    FileNameList.Add(NewName);
                }
                name = name + 1;//用来重命名规则的变量
            }
            return FileNameList;
        }
    }
}
